package com.nest.WorkNest.tools;

import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 数据存储工具
 * 
 * @author suligen
 *
 */
public class FileHelper {

	private SharedPreferences sharePref;

	public FileHelper(Context context, String fileName) {
		sharePref = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
	}

	public String getShareProf(String key) {
		return sharePref.getString(key, "");
	}

	public void putShareProf(String key, String value) {
		sharePref.edit().putString(key, value).commit();
	}

	/**
	 * 将json数据以键值对形式存储下来
	 * 
	 * @param context
	 * @param fileName
	 * @param json
	 */
	public static void savePrefByKeyValue(Context context, String fileName, String json) {
		if (json.length() == 0)
			return;
		SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		Editor edite = sp.edit();
		JSONArray array = new JSONArray();
		try {
			if (json.trim().startsWith("[") && json.trim().endsWith("]")) {
				array = new JSONArray(json);
			} else if (json.trim().startsWith("{") && json.trim().endsWith("}")) {
				array.put(new JSONObject(json));
			}
			for (int i = 0; i < array.length(); i++) {
				Iterator<?> it = array.getJSONObject(i).keys();
				while (it.hasNext()) {
					String key = (String) it.next();
					String value = array.getJSONObject(i).getString(key);
					edite.putString(key, value);
				}
			}
		} catch (Exception e) {
		}
		edite.commit();
	}

	public static String getShareProf(Context context, String fileName, String key) {
		SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		return sp.getString(key, "");
	}

	public static void putShareProf(Context context, String fileName, String key, String value) {
		SharedPreferences sp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
		sp.edit().putString(key, value).commit();
	}

	public static void deletedSpFile(Context context, String spFileName) {
		SharedPreferences sharePref = context.getSharedPreferences(spFileName,
				Context.MODE_PRIVATE);
		sharePref.edit().clear().commit();
	}

	/**
	 * put string preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to modify
	 * @param value
	 *            The new value for the preference
	 * @return True if the new values were successfully written to persistent
	 *         storage.
	 */
	public void putString(String key, String value) {
		sharePref.edit().putString(key, value).commit();
	}

	/**
	 * get string preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to retrieve
	 * @return The preference value if it exists, or null. Throws
	 *         ClassCastException if there is a preference with this name that
	 *         is not a string
	 * @see #getString(Context, String, String)
	 */
	public String getString(String key) {
		return getString(key, "");
	}

	/**
	 * get string preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to retrieve
	 * @param defaultValue
	 *            Value to return if this preference does not exist
	 * @return The preference value if it exists, or defValue. Throws
	 *         ClassCastException if there is a preference with this name that
	 *         is not a string
	 */
	public String getString(String key, String defaultValue) {
		return sharePref.getString(key, defaultValue);
	}

	/**
	 * put int preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to modify
	 * @param value
	 *            The new value for the preference
	 * @return True if the new values were successfully written to persistent
	 *         storage.
	 */
	public void putInt(String key, int value) {
		sharePref.edit().putInt(key, value).commit();
	}

	/**
	 * get int preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to retrieve
	 * @return The preference value if it exists, or -1. Throws
	 *         ClassCastException if there is a preference with this name that
	 *         is not a int
	 * @see #getInt(Context, String, int)
	 */
	public int getInt(String key) {
		return getInt(key, -1);
	}

	/**
	 * get int preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to retrieve
	 * @param defaultValue
	 *            Value to return if this preference does not exist
	 * @return The preference value if it exists, or defValue. Throws
	 *         ClassCastException if there is a preference with this name that
	 *         is not a int
	 */
	public int getInt(String key, int defaultValue) {

		return sharePref.getInt(key, defaultValue);
	}

	/**
	 * put long preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to modify
	 * @param value
	 *            The new value for the preference
	 * @return True if the new values were successfully written to persistent
	 *         storage.
	 */
	public void putLong(String key, long value) {
		sharePref.edit().putLong(key, value).commit();
	}

	/**
	 * get long preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to retrieve
	 * @return The preference value if it exists, or -1. Throws
	 *         ClassCastException if there is a preference with this name that
	 *         is not a long
	 * @see #getLong(Context, String, long)
	 */
	public long getLong(String key) {
		return getLong(key, -1);
	}

	/**
	 * get long preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to retrieve
	 * @param defaultValue
	 *            Value to return if this preference does not exist
	 * @return The preference value if it exists, or defValue. Throws
	 *         ClassCastException if there is a preference with this name that
	 *         is not a long
	 */
	public long getLong(String key, long defaultValue) {
		return sharePref.getLong(key, defaultValue);
	}

	/**
	 * put float preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to modify
	 * @param value
	 *            The new value for the preference
	 * @return True if the new values were successfully written to persistent
	 *         storage.
	 */
	public void putFloat(String key, float value) {
		sharePref.edit().putFloat(key, value).commit();
	}

	/**
	 * get float preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to retrieve
	 * @return The preference value if it exists, or -1. Throws
	 *         ClassCastException if there is a preference with this name that
	 *         is not a float
	 * @see #getFloat(Context, String, float)
	 */
	public float getFloat(String key) {
		return getFloat(key, -1);
	}

	/**
	 * get float preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to retrieve
	 * @param defaultValue
	 *            Value to return if this preference does not exist
	 * @return The preference value if it exists, or defValue. Throws
	 *         ClassCastException if there is a preference with this name that
	 *         is not a float
	 */
	public float getFloat(String key, float defaultValue) {
		return sharePref.getFloat(key, defaultValue);
	}

	/**
	 * put boolean preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to modify
	 * @param value
	 *            The new value for the preference
	 * @return True if the new values were successfully written to persistent
	 *         storage.
	 */
	public void putBoolean(String key, boolean value) {
		sharePref.edit().putBoolean(key, value).commit();
	}

	/**
	 * get boolean preferences, default is false
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to retrieve
	 * @return The preference value if it exists, or false. Throws
	 *         ClassCastException if there is a preference with this name that
	 *         is not a boolean
	 * @see #getBoolean(Context, String, boolean)
	 */
	public boolean getBoolean(String key) {
		return getBoolean(key, false);
	}

	/**
	 * get boolean preferences
	 * 
	 * @param context
	 * @param key
	 *            The name of the preference to retrieve
	 * @param defaultValue
	 *            Value to return if this preference does not exist
	 * @return The preference value if it exists, or defValue. Throws
	 *         ClassCastException if there is a preference with this name that
	 *         is not a boolean
	 */
	public boolean getBoolean(String key, boolean defaultValue) {
		return sharePref.getBoolean(key, defaultValue);
	}

}
